package com.prs.crm.action.cdxh;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.domain.cdxh.HrPerson;
import com.prs.crm.domain.cdxh.MaterialProduct;
import com.prs.crm.domain.cdxh.cd_Product;
import com.prs.crm.domain.cdxh.production.ProvideRegime;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.cdxh.HrPersonService;
import com.prs.crm.service.cdxh.MaterialProductService;
import com.prs.crm.service.cdxh.ProvideRegimeService;
import com.prs.crm.service.cdxh.cd_ProductService;
import com.prs.crm.service.hr.PersonService;

/**
 * @author LanXiaoGang
 * @E-mail:lan9liang@163.com
 * @qq:7857553326
 * @version 创建时间：2010-12-3 下午05:25:01 简单说明
 */

public class MaterialProductAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private HrPerson hrPerson;

	private String rowdata;

	private String productCode;

	private HrPersonService hrPersonService;

	private Person person;

	private cd_Product cd_Product;

	private cd_ProductService cd_ProductService;

	private PersonService personService;

	private MaterialProduct materialProduct;

	private ProvideRegimeService provideRegimeService;

	private ProvideRegime provideRegime;

	private MaterialProductService materialProductService;

	private List<MaterialProduct> mpList = new ArrayList<MaterialProduct>();

	private PaginationSupport pagination;

	private Integer[] ids;
	
	private String allids="";
	
	private String pagetrunNum="";
	/**
	 * 传值
	 */
	private String idInput = "";

	private String presentTime = "2010年10月10日";

	@Action("pLMachineClass")
	public String pLMachineClass() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ProvideRegime.class);

		criteria.add(Restrictions.eq("productStyle", "配料"));
		criteria.add(Restrictions.eq("inserValue", "1"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getProvideRegimeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("pLProductPerson")
	public String pLProductPerson() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				HrPerson.class);
		criteria.add(Restrictions.eq("hrDepartment", "配料组"));
		
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getHrPersonService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("AllProductPerson")
	public String AllProductPerson() {
	
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				HrPerson.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getHrPersonService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("listMaterialProduct")
	public String listMaterialProduct() {		
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MaterialProduct.class);
		criteria.add(Restrictions.lt("pLState", 7));
		criteria.add(Restrictions.eq("NumConfirmState",1));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMaterialProductService().listByPage(
				criteria, pagination));
		return SUCCESS;
	}

	@Action("editMaterialProduct")
	public String editMaterialProduct() {
		if(pagetrunNum==null || "".equals(pagetrunNum)){
			
		}
		String[] ss=pagetrunNum.split(",");
		for(int j=0;j<ss.length;j++){
			if(ss[j]!=""){
		
			MaterialProduct mpl = this.getMaterialProductService().get(Integer.parseInt(ss[j]));
			mpList.add(mpl);
			if ("".equals(idInput)) {
				idInput = ss[j];
			} else {
				idInput = idInput + "," + ss[j];
			}
			}
		}
//		for (int i = 0; i < ids.length; i++) {
//			MaterialProduct mpl = this.getMaterialProductService().get(ids[i]);
//			mpList.add(mpl);
//			if ("".equals(idInput)) {
//				idInput = ids[i] + "";
//			} else {
//				idInput = idInput + "," + ids[i];
//			}
//
//		}

		presentTime = getPresentTimes();
		return SUCCESS;
	}

	/**
	 * 根据产品编号查询是否已安全配置
	 * 
	 * @return
	 */
	@Action(value = "serchProductCode", results = { @Result(name = "success", type = "json", params = {
			"root", "rowdata" }) })
	public String serchProductCode() {
		cd_Product   cpOnobject=this.getCd_ProductService().getProductObject(productCode);
		String bk=cpOnobject.getShowSafeConfigration()+"";
		this.rowdata = bk;
		return SUCCESS;
	}

	public MaterialProduct getMaterialProduct() {
		return materialProduct;
	}

	public void setMaterialProduct(MaterialProduct materialProduct) {
		this.materialProduct = materialProduct;
	}

	public MaterialProductService getMaterialProductService() {
		return materialProductService;
	}

	public void setMaterialProductService(
			MaterialProductService materialProductService) {
		this.materialProductService = materialProductService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public List<MaterialProduct> getMpList() {
		return mpList;
	}

	public void setMpList(List<MaterialProduct> mpList) {
		this.mpList = mpList;
	}

	public String getPresentTime() {
		return presentTime;
	}

	public void setPresentTime(String presentTime) {
		this.presentTime = presentTime;
	}

	public ProvideRegimeService getProvideRegimeService() {
		return provideRegimeService;
	}

	public void setProvideRegimeService(
			ProvideRegimeService provideRegimeService) {
		this.provideRegimeService = provideRegimeService;
	}

	public ProvideRegime getProvideRegime() {
		return provideRegime;
	}

	public void setProvideRegime(ProvideRegime provideRegime) {
		this.provideRegime = provideRegime;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public HrPerson getHrPerson() {
		return hrPerson;
	}

	public void setHrPerson(HrPerson hrPerson) {
		this.hrPerson = hrPerson;
	}

	public HrPersonService getHrPersonService() {
		return hrPersonService;
	}

	public void setHrPersonService(HrPersonService hrPersonService) {
		this.hrPersonService = hrPersonService;
	}

	String getPresentTimes() {
		int yyyy, MM, dd;
		Calendar c = Calendar.getInstance();
		yyyy = c.get(Calendar.YEAR);
		MM = c.get(Calendar.MONTH) + 1;
		dd = c.get(Calendar.DAY_OF_MONTH);
		String Times = yyyy + "年" + MM + "月" + dd + "日";
		return Times;
	}

	public String getIdInput() {
		return idInput;
	}

	public void setIdInput(String idInput) {
		this.idInput = idInput;
	}

	public String getRowdata() {
		return rowdata;
	}

	public void setRowdata(String rowdata) {
		this.rowdata = rowdata;
	}

	public String getProductCode() {
		return productCode;
	}

	public void setProductCode(String productCode) {
		this.productCode = productCode;
	}

	public cd_Product getCd_Product() {
		return cd_Product;
	}

	public void setCd_Product(cd_Product cd_Product) {
		this.cd_Product = cd_Product;
	}

	public cd_ProductService getCd_ProductService() {
		return cd_ProductService;
	}

	public void setCd_ProductService(cd_ProductService cd_ProductService) {
		this.cd_ProductService = cd_ProductService;
	}

	public String getAllids() {
		return allids;
	}

	public void setAllids(String allids) {
		this.allids = allids;
	}

	public String getPagetrunNum() {
		return pagetrunNum;
	}

	public void setPagetrunNum(String pagetrunNum) {
		this.pagetrunNum = pagetrunNum;
	}

}
